/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.toedo.gui.translations.Messages;
import org.toedo.model.Attachment;
import org.toedo.model.BaseNote;
import org.toedo.model.Attachment.AttachmentType;

public class EditAttachmentDialog extends TitleAreaDialog {

	private BaseNote mBaseNote;
	
	private Text mTitleText;
	private Text mLinkText;
	private Button mBrowseLinkButton;
	
	private AttachmentType mType;
	private String mTitle;
	private String mLink;
	
	public EditAttachmentDialog(Shell parentShell, BaseNote baseNote, Attachment attachment) {
		super(parentShell);
		mBaseNote = baseNote;
		
		if (attachment != null) {
			mType = attachment.getType();
			mTitle = attachment.getTitle();
			mLink = attachment.getLink();
		} else {
			mType = AttachmentType.EnFile;
			mTitle = ""; //$NON-NLS-1$
			mLink = ""; //$NON-NLS-1$
		}
	}
	
	public EditAttachmentDialog(Shell parentShell, BaseNote baseNote) {
		this(parentShell, baseNote, null);
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		
		return contents;
	}
	
	protected Control createDialogArea(final Composite parent) {
		setTitle(Messages.EditAttachmentDialog_Title);		
		setMessage(String.format(Messages.EditAttachmentDialog_Message, mBaseNote.getTitle()));
		
		Composite mainPanel = (Composite) super.createDialogArea(parent);
		mainPanel.setLayout(new GridLayout(1, false));
		
		GridData mainGD = new GridData(SWT.FILL, SWT.FILL, true, true);
		//mainGD.heightHint = 300;
		mainPanel.setLayoutData(mainGD);
		
		Composite panel = new Composite(mainPanel, SWT.NONE);
		panel.setLayout(new GridLayout(3, false));
		panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		Label titleLabel = new Label(panel, SWT.NONE);
		titleLabel.setText(Messages.EditAttachmentDialog_TitleLabel);
		
		mTitleText = new Text(panel, SWT.BORDER);
		mTitleText.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 2, 1));
		
		Group linkTypeGroup = new Group(panel, SWT.NONE);
		linkTypeGroup.setLayout(new GridLayout(1, false));
		linkTypeGroup.setText(Messages.EditAttachmentDialog_TypeLabel);
		linkTypeGroup.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 3, 1));
		
		Button fileTypeButton = new Button(linkTypeGroup, SWT.RADIO);
		fileTypeButton.setText(Messages.EditAttachmentDialog_FileChoice);
		fileTypeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				mType = AttachmentType.EnFile;
				mLinkText.setEnabled(false);
				mBrowseLinkButton.setVisible(true);
			}
		});
		
		Button linkTypeButton = new Button(linkTypeGroup, SWT.RADIO);
		linkTypeButton.setText(Messages.EditAttachmentDialog_LinkChoice);
		linkTypeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				mType = AttachmentType.EnUrl;
				mLinkText.setEnabled(true);
				mBrowseLinkButton.setVisible(false);
			}
		});
		
		Label linkLabel = new Label(panel, SWT.NONE);
		linkLabel.setText(Messages.EditAttachmentDialog_LinkLabel);
		
		mLinkText = new Text(panel, SWT.BORDER);
		mLinkText.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		
		mBrowseLinkButton = new Button(panel, SWT.PUSH);
		mBrowseLinkButton.setText(Messages.EditAttachmentDialog_BrowseButton);
		mBrowseLinkButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				FileDialog dialog = new FileDialog(parent.getShell(), SWT.OPEN);
			    dialog.setFilterExtensions(new String[] {"*.*"}); //$NON-NLS-1$
			    dialog.setFilterNames(new String[] {Messages.OpenAction_AllFilesFilterLabel});
			    String fileSelected = dialog.open();
			    
			    if ((fileSelected != null) &&
			    		(!fileSelected.isEmpty())) {
			    	mLinkText.setText(fileSelected);
			    }
			}
		});
		
		switch (mType) {
		case EnFile : {
			fileTypeButton.setSelection(true);
			mLinkText.setEnabled(false);
			mBrowseLinkButton.setVisible(true);
			break;
		}
		case EnUrl : {
			linkTypeButton.setSelection(true);
			mLinkText.setEnabled(true);
			mBrowseLinkButton.setVisible(false);
			break;
		}
		}
		
		mTitleText.setText(mTitle);
		mLinkText.setText(mLink);
		
		mTitleText.setFocus();
		mTitleText.setSelection(0, mTitleText.getText().length());
		
		return parent;
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		Button okBtn = createButton(parent, -1, IDialogConstants.OK_LABEL, true);
		Button closeBtn = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		
		okBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				
				boolean doClose = true;
				
				if (mTitleText.getText().isEmpty()) {
					setErrorMessage(Messages.EditAttachmentDialog_ErrorTitleEmpty);
					doClose = false;
				} else if (mLinkText.getText().isEmpty()) {
					setErrorMessage(Messages.EditAttachmentDialog_ErrorLinkEmpty);
					doClose = false;
				} 
				
				if (doClose) {
					mTitle = mTitleText.getText();
					mLink = mLinkText.getText();
					close();
				}
			}
		});
		
		closeBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {				
				close();				
			}
		});
	}
	
	public AttachmentType getType() {
		return mType;
	}
	
	public String getTitle() {
		return mTitle;
	}
	
	public String getLink() {
		return mLink;
	}

}
